package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.GetOrderResponse;

/**
 * Created by ASUS on 2017/11/6.
 */

public class ChildOrderAdapter extends BaseAdapter {
    private GetOrderResponse getOrderResponse;
    private Context mContext;
    public ChildOrderAdapter(Context context, GetOrderResponse getOrderResponse){
        this.getOrderResponse = getOrderResponse;
        mContext = context;
    }

    public void setDatas(GetOrderResponse getOrderResponse){
        this.getOrderResponse = getOrderResponse;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.order_item_layout, parent, false);
            holder = new ViewHolder();
            holder.money = view.findViewById(R.id.money);
            holder.name = view.findViewById(R.id.name);
            holder.number = view.findViewById(R.id.number);
            holder.type = view.findViewById(R.id.type);
            holder.descp = view.findViewById(R.id.descp);
            holder.cup = view.findViewById(R.id.cup);
            view.setTag(holder);
        }

        holder.name.setText(getOrderResponse.getList().get(position).getName());
        holder.number.setText(getOrderResponse.getList().get(position).getPeriod_no() + "期");
        holder.money.setText("-" + getItem(position).getAmount() + "元");
        if (getItem(position).getOrder_status().equals("2") || getItem(position).getOrder_status().equals("1")) {
            holder.cup.setVisibility(View.VISIBLE);
        } else {
            holder.cup.setVisibility(View.GONE);
        }
        holder.type.setText(Html.fromHtml("<font color='#FF4081'>" + getItem(position).getPick_number() + "</font> " + getItem(position).getItem_name_abbreviation()));
        switch (getItem(position).getOrder_status()) {
            case "2":  // 已中獎
                holder.descp.setText("中奖" + getItem(position).getWin_amount() + "元");
                holder.descp.setTextColor(Color.parseColor("#f45300"));
                view.setBackgroundResource(R.drawable.bg_corners_100_11);
                holder.cup.setImageResource(R.mipmap.win);
                break;
            case "1":
                holder.descp.setTextColor(Color.parseColor("#d69313"));
                holder.descp.setText("待开奖");
                view.setBackgroundResource(R.drawable.bg_corners_19);
                holder.cup.setImageResource(R.mipmap.gift);
                break;
            default:  // 3: 未中獎, 4: 己取消, 5: 己撤單
                holder.descp.setText(getItem(position).getStatus());
                view.setBackgroundResource(R.drawable.bg_corners_100_11);
                holder.descp.setTextColor(Color.parseColor("#989898"));
                break;
        }

        return view;
    }

    @Override
    public int getCount() {
        if (getOrderResponse == null || getOrderResponse.getList() == null) {
            return 0;
        }
        return getOrderResponse.getList().size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public GetOrderResponse.ListBean getItem(int i) {
        if(getOrderResponse == null || getOrderResponse.getList() == null){
            return null;
        }
        return getOrderResponse.getList().get(i);
    }

    static class ViewHolder {
        TextView name;
        TextView number;
        TextView money;
        TextView type;
        TextView descp;
        ImageView cup;
    }
}
